<?php
class Model_RealEstate
{

    public static function getAllRealIds()
    {
        $cache = Core_Global::getCache();
        $listIds = $cache->get('Real:ListIds');

        if (empty($listIds)) {
            $arrData = Business_RealEstate::getAllId();
            $listIds = array();

            foreach($arrData as $data)
            {
                $listIds[] = $data['realEstateID'];
            }

            $cache->set('Real:ListIds', $listIds, 604800);
        }

        return $listIds;
    }

    public static function getAll($params)
    {
        $listId = self::getAllRealIds();
        $temp = array_splice($listId, $params['start'], $params['end']);
        if (empty($temp))
        {
            return null;
        }
        return self::getAllIds($temp);
    }

    public static function getAllIds($listIds)
    {
        try
        {
            $cache = Core_Global::getCache();
            $cacheKeys = array();

            foreach($listIds as $id)
            {
                $cacheKeys[] = 'Real:id:' . $id;
            }

            $result = $cache->getMulti($cacheKeys);

            //Miss cache
            if ($result === false || count($result) != count($cacheKeys))
            {
                if ($result === false) $result = array();

                $missKeys = array_diff(array_keys(array_flip($cacheKeys)), array_keys($result));

                array_walk($missKeys, function(&$item){
                    $item = explode(':', $item);
                    $item = $item[count($item) - 1];
                });

                //Get result from database
                $missResult = Business_RealEstate::getAllRealEstateByIds(implode(',', $missKeys));
                var_dump($missResult);
                foreach($missResult as $location)
                {
                    $cache->set('Real:id:' . $location['realEstateID'], $location, 604800);
                }

                $result = array_merge($missResult,$result);
            }

            return $result;
        }
        catch(Zend_Exception $e)
        {
            var_dump($e->getMessage());
        }
    }



    public static function create($params)
    {

        $result = Business_RealEstate::create($params);
        if ($result) {

            //Business_GmapLocation::create($params)
            $cache = Core_Global::getCache();
            $cache->delete('Real:ListIds');

        }
        return $result;
    }

    public static function update($params)
    {
        $result = Business_SaleCategory::update($params);
        if ($result) {
            $cache = Core_Global::getCache();
            $cache->delete('Real:id:' . $params['RealEstateID']);
        }
        return $result;
    }

    public static function delete($realID)
    {
        $result = Business_RealEstate::delete($realID);
        if ($result) {
            $cache = Core_Global::getCache();

            $cache->delete('Real:ListIds');
        }
        return $result;
    }
    public static function toCoordinates($address)
    {
        $bad = array(
            " " => "+",
            "," => "",
            "?" => "",
            "&" => "",
            "=" => ""
        );
        $address = str_replace(array_keys($bad), array_values($bad), $address);
        $data = new SimpleXMLElement(file_get_contents("http://maps.google.com/maps/geo?output=xml&q={$address}"));
        $coordinates = explode(",", $data->Response->Placemark->Point->coordinates);
        return array(
            "latitude" => $coordinates[0],
            "longitude" => $coordinates[1]
        );
    }
    public static function find($params)
    {
        return Business_SaleCategory::find($params);
    }
}